#! /usr/bin/env python

import socket
import sys
import select
import pyglet
from pyglet.window import Window
from pyglet.window import key
from pyglet import clock
from pyglet import font
from struct import *

#------------Networking stuff------------------

login_cred_file = open("data/login_cred", "r")
login_cred = login_cred_file.readline()
login_cred_lines = login_cred.split("=")
temp = 0
login_username = ""
login_password = ""
login_username = login_cred_lines[0]
login_password = login_cred_lines[1]
#for line in login_cred_lines:
#	if temp == 0:
#		login_username = line
#		temp ++
#	if temp == 1:
#		login_password = line
#		temp ++

print("Username: "+login_username)
print("Password: "+login_password)
command = "\x00" #The data it sends to the server.
		
	

server_file = open("data/servers.txt", "r")
server_name = server_file.readline()
server_port = 1200
server_name = server_name.rstrip()
print("Connecting to mug server \""+server_name+"\"")

def connect_server():
	clisock.connect( (server_name, server_port) )
#	clisock.send("Hello World\n")
	print clisock.recv(100)
	clisock.send("username="+login_username+"="+login_password)
#	clisock.send(login_password)

clisock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )

connect_server()

def send_string(str):
	clisock.send(str)
	print "Sent string: " + str

def check_packets():
	print "Recieved package. Contents:"
	print clisock.recv(100)
	pass


#	clisock.close()



#----------------------------------------------



#------------Important junk--------------------

def center_image(image):
	image.anchor_x = image.width/2
	image.anchor_y = image.height/2

def parse_game_string(data):
	lines = data.split("\n")
	for line in lines:
	 brokenline = line.split("=")
	 if brokenline[0] == "player_health":
	  player_health = brokenline[1]
	 elif brokenline[0] == "player_magic":
	  player_magic = brokenline[1]
	 #etc

pyglet.resource.path = ['.']
window = pyglet.window.Window(800, 600)

direction = 0 #This is the direction the player is moving in. 0 = still. 1 = north. 2 = south. 3 = east. 4 = west.

@window.event
def on_draw():
	window.clear()

@window.event
def on_close():
    clisock.close()

@window.event
def on_key_press(key, modifiers):
	if key == pyglet.window.key.UP:
	 direction = 1
	 print "Key up pressed"
	 send_string("\x02\x00")

	elif key == pyglet.window.key.DOWN:
	 direction = 2
	 print "Key down pressed"
	 send_string("\x02\x02")

	elif key == pyglet.window.key.RIGHT:
	 direction = 3
	 print "Key right presse"
	 send_string("\x02\x01")

	elif key == pyglet.window.key.LEFT:
	 direction = 4
	 print "Key left pressed"
	 send_string("\x02\x03")

@window.event
def on_key_release(key, modifiers):
	if key in (pyglet.window.key.UP, pyglet.window.key.DOWN, pyglet.window.key.RIGHT, pyglet.window.key.LEFT):
	 direction = 0
	 print "No key pressed."

#----------------------------------------------

#----------------The main game loop------------

def update():
	print "updating"

#----------------------------------------------

# Schedule a function call in Pyglet
def runEverySecondPyglet(dt):
#	print "pyglet call: one second has passed"
	#print "Checking if any packets have been recieved from the server..."
	#check_packets()
	update() #Calls the main game loop
	pass
pyglet.clock.schedule_interval(runEverySecondPyglet, 1)

pyglet.app.run()

#send_string('')

#clisock.shutdown(socket.SHUT_RDWR)
clisock.close()
print "Exited"
